﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace SmartSpace.Silverlight
{
    public class ArcPanel : Grid
    {
        //private List<UIElement> m_buttons = new List<UIElement>();

        public ArcPanel()
        {
            this.LayoutUpdated += new EventHandler(CirclePanel_LayoutUpdated);
            this.Loaded += new RoutedEventHandler(CirclePanel_Loaded);
            CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);
        }

        private int m_displayIndex = 0;
        private int m_step = 0;

        void CirclePanel_Loaded(object sender, RoutedEventArgs e)
        {
        }
        public void EndChildrenAdd()
        {
            m_displayIndex = 0;
            m_step = 1;
            foreach (UIElement element in this.Children)
            {
                element.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        void CompositionTarget_Rendering(object sender, EventArgs e)
        {
            if (m_displayIndex < this.Children.Count)
            {
                if (m_step > 0)
                {
                    m_step--;
                }
                else
                {
                    double cx = this.ActualWidth / 2;
                    double cy = this.ActualHeight;

                    double angle = Math.PI / (this.Children.Count+1);
                    double dy = this.ActualHeight / (this.Children.Count + 1);
                    double dx = this.ActualWidth / (this.Children.Count + 1);
   
                    for (int index = 0; index < this.Children.Count; index++)
                    {
                        double a = Math.PI + angle * (index+1);
                        double x = dx * (index + 1);
                        double y = cy * Math.Sin(a);
                        FrameworkElement btn = (this.Children[index] as FrameworkElement);
                        btn.VerticalAlignment = System.Windows.VerticalAlignment.Top;
                        btn.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;

                        double fy = cy + y;
                        if (fy > this.ActualHeight - btn.Height)
                        {
                            fy = this.ActualHeight - btn.Height;
                        }
                        btn.Margin = new Thickness(
                            x - btn.ActualWidth / 2, fy, 0, 0);

                        if (index == m_displayIndex)
                        {
                            btn.Visibility = System.Windows.Visibility.Visible;
                            m_displayIndex++;
                            m_step = 1;
                            break;
                        }
                    }
                }
            }
        }

        void CirclePanel_LayoutUpdated(object sender, EventArgs e)
        {
            double cx = this.ActualWidth / 2;
            double cy = this.ActualHeight;

            double angle = Math.PI / (this.Children.Count + 1);
            double dy = this.ActualHeight / (this.Children.Count + 1);
            double dx = this.ActualWidth / (this.Children.Count + 1);

            for (int index = 0; index < this.Children.Count; index++)
            {
                double a = Math.PI + angle * (index + 1);
                double x = dx * (index + 1);
                double y = cy * Math.Sin(a);
                FrameworkElement btn = (this.Children[index] as FrameworkElement);
                btn.VerticalAlignment = System.Windows.VerticalAlignment.Top;
                btn.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;

                double fy = cy + y;
                if (fy > this.ActualHeight - btn.Height)
                {
                    fy = this.ActualHeight - btn.Height;
                }
                btn.Margin = new Thickness(
                    x - btn.ActualWidth / 2, fy, 0, 0);
            }
        }

    }

    
}
